package net.qiqbframework.spring.command;


import net.qiqbframework.config.CommandConfigurer;
import net.qiqbframework.config.Configurer;
import net.qiqbframework.config.ConfigurerModule;
import net.qiqbframework.modelling.repository.Repository;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.Nonnull;

public class GeneralCommandConfigurer<C, A> implements ConfigurerModule, ApplicationContextAware {

    private final Class<C> commandType;
    private final Class<A> aggregateType;

    private String aggregateRepository;

    private String commandTargetResolver;

    private ApplicationContext applicationContext;

    public GeneralCommandConfigurer(Class<C> commandType, Class<A> aggregateType) {
        this.commandType = commandType;
        this.aggregateType = aggregateType;

    }

    /**
     * Sets the bean name of the {@link Repository} to configure for this Aggregate.
     *
     * @param aggregateRepository The bean name of the {@link Repository} for this Aggregate.
     */
    public void setRepository(String aggregateRepository) {
        this.aggregateRepository = aggregateRepository;
    }


    public void setCommandTargetResolver(String commandTargetResolver) {
        this.commandTargetResolver = commandTargetResolver;
    }

    @Override
    public void configureModule(@Nonnull Configurer configurer) {
        CommandConfigurer<C, A> aggregateConfigurer = CommandConfigurer.defaultConfiguration(commandType, aggregateType);


        configurer.registerModule(aggregateConfigurer);
    }


    @Override
    public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
